package com.dayuanit.dy15.ebook.bookshop.controller;

import com.dayuanit.dy15.ebook.bookshop.dto.ResponseDTO;
import com.dayuanit.dy15.ebook.bookshop.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

public abstract class BaseController {

    //原本HttpSession是有状态的，不是线程安全的；
    // 但是，spring帮我们代理了一个线程安全的 HttpSession
    //所以，可以将其提升为成员变量，这样就不用每次都传递HttpSession对象了。
    @Autowired
    protected HttpSession session;

    public long getUserId() {
        return 1000;
    }

    @ExceptionHandler(BizException.class)
    protected ResponseDTO processBizException(BizException be) {
        be.printStackTrace();
        return ResponseDTO.fail(be.getMessage());
    }

    @ExceptionHandler(Exception.class)
    protected ResponseDTO processException(Exception be) {
        be.printStackTrace();
        return ResponseDTO.fail("xxxx");
    }
}
